package com.gourd.quartz.controller;

import com.gourd.quartz.job.Job;
import com.gourd.quartz.service.DeleteTempFileSchedule;
import com.gourd.quartz.service.QuartzService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 供前期测试使用
 * @author gourd
 */
@RestController
@RequestMapping("/quartz")
@Api(tags = "quartz", description = "定时器控制器")
@Slf4j
@ConditionalOnProperty(name = "schedule.gourd.enabled", havingValue = "true")
public class QuartzController {

    @Autowired
    private QuartzService quartzService;

    @Autowired
    private DeleteTempFileSchedule scheduledService;


    @PostMapping("/job-add")
    @ApiOperation(value = "增加定时任务")
    public void addJob() {
        Map map = new HashMap(2);
        map.put("id",1L);
        // 先删除，再新增加
        quartzService.deleteJob("job", "test");
        quartzService.addJob(Job.class, "job", "test", "0/30 * * * * ?",map);
    }
    
    @PutMapping("/job-update")
    @ApiOperation(value = "更新定时任务")
    public void updateJob() {
            quartzService.updateJob("job", "test", "0/10 * * * * ?");
    }
    
    @PostMapping("/job-delete")
    @ApiOperation(value = "删除定时任务")
    public void deleteJob() {
            quartzService.deleteJob("job", "test");
    }
    
    @PutMapping("/job-pause")
    @ApiOperation(value = "暂停定时任务")
    public void pauseJob() {
            quartzService.pauseJob("job", "test");
    }
    
    @PutMapping("/job-resume")
    @ApiOperation(value = "恢复定时任务")
    public void resumeJob() {
            quartzService.resumeJob("job", "test");
    }
    
    @GetMapping("/all-job")
    @ApiOperation(value = "查询所有定时任务")
    public Object queryAllJob() {
            return quartzService.queryAllJob();
    }
    

    @GetMapping("/job-run")
    @ApiOperation(value = "查询定时任务")
    public Object queryRunJob() {
            return quartzService.queryRunJob();
    }


    @PutMapping("/task-update")
    @ApiOperation(value = "修改task任务")
    public void getCron(String name,String cron,String jobData) {
        scheduledService.setName(name);
        scheduledService.setCron(cron);
        scheduledService.setJobData(jobData);
    }


}